#include "Bullet.h" 
#include "TiledMapInfo.h"
#include "AnimationUtil.h"
#include "cocostudio\CocoStudio.h"
#include "ui\CocosGUI.h"
#include "BulletManager.h"
#include "SimpleAudioEngine.h"

Bullet::Bullet(){
	isPlayer = false;
}

Bullet::~Bullet(){
}

bool Bullet::init(){
	
	return true;
}

Bullet* Bullet::createWithNum(int type, int powerLevel, float rotation, Vec2 position){
	auto bullet = new Bullet();
	if (bullet && bullet->initWithNum(type, powerLevel, rotation, position)){
		bullet->autorelease();
	}
	else{
		CC_SAFE_DELETE(bullet);
	}
	
	return bullet;
}

bool Bullet::initWithNum(int type, int powerLevel, float rotation, Vec2 position){
	auto sprite = Sprite::create(StringUtils::format("tank/bullet%d.png", type));
	this->setScale(0.5);
	this->bindNode(sprite);
	m_rotation = rotation;
	this->setTagRotation(rotation);
	this->setPosition(position);
	return true;
}

void Bullet::boom(){
	if (m_controller == NULL) return;
	m_controller->removeFromParentAndCleanup(true);
	m_controller = NULL;
	this->removeFromParentAndCleanup(true);
}

bool Bullet::setTagPosition(float x, float y){
	this->getBox(x, y);
	for (auto dstPos : a){
		Vec2 tiledPos = TiledMapInfo::tileCoorForPosition(m_map ,Vec2(dstPos.x, dstPos.y));

		int tiledGid = meta->getTileGIDAt(tiledPos);

		if (tiledGid != 0){
			Value properties = m_map->getPropertiesForGID(tiledGid);
			ValueMap propertiesMap = properties.asValueMap();

			if (propertiesMap.find("Collidable") != propertiesMap.end()){
				Value prop = propertiesMap.at("Collidable");
				if (prop.asString().compare("true") == 0){
					this->boom();
					return false;
				}
			}
		}
	}
	this->setPosition(x, y);
	return true;
}

void Bullet::setIsPlayer(bool b){
	isPlayer = b;
}

bool Bullet::getIsPlayer(){
	return isPlayer;
}